package cn.iocoder.yudao.module.shop.controller.admin.item;

import cn.iocoder.yudao.module.shop.controller.admin.itemconsume.ShopItemConsumeCreateReqVO;
import cn.iocoder.yudao.module.shop.controller.admin.itemconsume.ShopItemConsumeRespVO;
import cn.iocoder.yudao.module.shop.controller.admin.itemconsume.ShopItemConsumeUpdateReqVO;
import cn.iocoder.yudao.module.shop.dal.dataobject.itemconsume.ShopItemConsumeDO;
import cn.iocoder.yudao.module.shop.dal.mysql.itemconsume.ShopItemConsumeMapper;
import cn.iocoder.yudao.module.shop.service.itemconsume.ShopItemConsumeService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.shop.controller.admin.item.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.item.ShopItemDO;
import cn.iocoder.yudao.module.shop.convert.item.ShopItemConvert;
import cn.iocoder.yudao.module.shop.service.item.ShopItemService;

@Tag(name = "管理后台 - 店铺项目")
@RestController
@RequestMapping("/shop/item")
@Validated
public class ShopItemController {

    @Resource
    private ShopItemService itemService;

    @Resource
    private ShopItemConsumeService shopItemConsumeService;

    @PostMapping("/create")
    @Operation(summary = "创建店铺项目")
    @PreAuthorize("@ss.hasPermission('shop:item:create')")
    public CommonResult<Long> createItem(@Valid @RequestBody ShopItemCreateReqVO createReqVO) {
        return success(itemService.createItem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新店铺项目")
    @PreAuthorize("@ss.hasPermission('shop:item:update')")
    public CommonResult<Boolean> updateItem(@Valid @RequestBody ShopItemUpdateReqVO updateReqVO) {
        itemService.updateItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除店铺项目")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('shop:item:delete')")
    public CommonResult<Boolean> deleteItem(@RequestParam("id") Long id) {
        itemService.deleteItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得店铺项目")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:item:query')")
    public CommonResult<ShopItemRespVO> getItem(@RequestParam("id") Long id) {
        ShopItemDO item = itemService.getItem(id);
        List<ShopItemConsumeRespVO> consumeRespVOList = shopItemConsumeService.getList(id);

        return success(ShopItemConvert.INSTANCE.convert(item, consumeRespVOList));
    }

    @GetMapping("/get-by-tenantids")
    @Operation(summary = "获得店铺项目列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('shop:item:query')")
    public CommonResult<List<ShopItemDO>> getByTenantIds(@RequestParam("ids") Collection<Long> ids){
        List<ShopItemDO> list = itemService.getItemByTenantIds(ids);
        return success(list);
    }

    @GetMapping("/list")
    @Operation(summary = "获得店铺项目列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('shop:item:query')")
    public CommonResult<List<ShopItemRespVO>> getItemList(@RequestParam("ids") Collection<Long> ids) {
        List<ShopItemDO> list = itemService.getItemList(ids);
        List<Long> itemIds = new ArrayList<>();
        list.forEach(shopItemDO -> {
            itemIds.add(shopItemDO.getId());
        });
        List<ShopItemConsumeRespVO> shopItemConsumeRespVOList = shopItemConsumeService.getList(itemIds);

        return success(ShopItemConvert.INSTANCE.convertList(list, shopItemConsumeRespVOList));
    }

    @GetMapping("/page")
    @Operation(summary = "获得店铺项目分页")
    @PreAuthorize("@ss.hasPermission('shop:item:query')")
    public CommonResult<PageResult<ShopItemRespVO>> getItemPage(@Valid ShopItemPageReqVO pageVO) {
        PageResult<ShopItemDO> pageResult = itemService.getItemPage(pageVO);
        List<Long> itemIds = new ArrayList<>();
        pageResult.getList().forEach(shopItemDO -> {
            itemIds.add(shopItemDO.getId());
        });
        List<ShopItemConsumeRespVO> shopItemConsumeRespVOList = shopItemConsumeService.getList(itemIds);

        return success(ShopItemConvert.INSTANCE.convertPage(pageResult, shopItemConsumeRespVOList));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出店铺项目 Excel")
    @PreAuthorize("@ss.hasPermission('shop:item:export')")
    @OperateLog(type = EXPORT)
    public void exportItemExcel(@Valid ShopItemExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ShopItemDO> list = itemService.getItemList(exportReqVO);
        // 导出 Excel
        List<ShopItemExcelVO> datas = ShopItemConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "店铺项目.xls", "数据", ShopItemExcelVO.class, datas);
    }

    ////////////////////////////////////////////////项目消耗/////////////////////////////////////////////////////////////

    @GetMapping("/consume/get")
    @Operation(summary = "获得项目消耗项")
    @Parameter(name = "id", description = "消耗编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:item:query')")
    public CommonResult<ShopItemConsumeRespVO> getConsumeItem(@RequestParam("id") Long id) {
        return success(shopItemConsumeService.getByConsumeId(id));
    }

    @PostMapping("/consume/create")
    @Operation(summary = "创建项目消耗")
    @PreAuthorize("@ss.hasPermission('shop:item:create')")
    public CommonResult<Boolean> consumeCreateItem(@Valid @RequestBody ShopItemConsumeCreateReqVO createReqVO) {
        shopItemConsumeService.createItem(createReqVO);
        return success(true);
    }

    @PutMapping("/consume/update")
    @Operation(summary = "更新项目消耗")
    @PreAuthorize("@ss.hasPermission('shop:item:update')")
    public CommonResult<Boolean> consumeUpdateItem(@Valid @RequestBody ShopItemConsumeUpdateReqVO updateReqVO) {
        shopItemConsumeService.updateItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/consume/delete")
    @Operation(summary = "删除项目消耗")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('shop:item:delete')")
    public CommonResult<Boolean> consumeDeleteItem(@RequestParam("id") Long id) {
        shopItemConsumeService.deleteItem(id);
        return success(true);
    }
}
